package org.thesis.activity;

import java.util.ArrayList;
import java.util.List;

import org.thesis.adapter.econtact_adapter;
import org.thesis.dataAccess.db_econtact;
import org.thesis.doituong.econtact;


import uit.mmt02.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class get_econtact_Activity extends Activity
{
	final static String KEY_CONTACT = "key_contact";
	final static int RESULT_GET_ECONTACT = 55;
	ListView listview ;
	Cursor mCursor;
	String tag = "get email_activity";
	String name;
	String phone;
	String email;
	String id;
	List<econtact>list;
	ArrayAdapter<econtact>adapter;
	db_econtact mydb;
	String str_email;
//	EditText txt_name, txt_email;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.getemail_layout);
		listview = (ListView) findViewById(R.id.lv_getemail);
		list = new ArrayList<econtact>();
//		txt_name = (EditText) findViewById(R.id.txt_getmail_name);
//		txt_email = (EditText) findViewById(R.id.txt_getmail_email);
		mydb = new db_econtact(this);
		str_email = "";
		mCursor = mydb.get_email_from_contact();
		convertToList();
		adapter = new econtact_adapter(this, list);
		if(list.size() < 1)
		{
			Toast.makeText(this, "Khong co email trong danh ba", Toast.LENGTH_SHORT).show();
		}
		
		listview.setAdapter(adapter);
		mCursor.close();
	}
	
	public void convertToList()
	{
		try
		{
			if(mCursor.moveToFirst())
			{
				do
				{
					name = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
					email = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
					econtact e1 = new econtact(name, email, false);
					list.add(e1);
				}while(mCursor.moveToNext());
			}
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "ERR convert " +e.toString());
		}
	}
	public void img_onClick(View v)
	{
		ImageButton img = (ImageButton)v;
		switch (img.getId()) {
		case R.id.img_getmail_backA:
			finish();
			break;
		case R.id.img_getmail_menu:
			openOptionsMenu();
			break;

		default:
			break;
		}
	}
	public void btn_onClick(View v)
	{
		Button btn = (Button)v;
		switch (btn.getId()) {
		case R.id.btn_getmail_duyet:
			ok();
			break;
		case R.id.btn_getmail_add:
			Intent i_add_new = new Intent(this, add_new_contact.class);
			startActivityForResult(i_add_new,0);
			break;
			

		default:
			break;
		}
	}
	
	public void ok()
	{
		try
		{
			Log.v(tag, "duyet");
			for(int i = 0; i < list.size(); i ++)
			{
				if(list.get(i).isCheck())
				{
					str_email += list.get(i).getEmail();
					str_email += ";";
				}
			}
			Intent intent = new Intent();
			intent.putExtra(KEY_CONTACT,str_email);
			this.setResult(RESULT_GET_ECONTACT, intent);
			Log.v(tag, "gui result : " + str_email);
			Toast.makeText(getBaseContext(), str_email, Toast.LENGTH_LONG).show();
			finish();
			
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "loi xac nhan " +e.toString());
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK)
		{
			Log.v(tag, "nhan dc result OK ...");
			list = new ArrayList<econtact>();
			mCursor = mydb.get_email_from_contact();
			convertToList();
			adapter = new econtact_adapter(this, list);
			if(list.size() < 1)
			{
				Toast.makeText(this, "Khong co email trong danh ba", Toast.LENGTH_SHORT).show();
			}
			
			listview.setAdapter(adapter);
//			adapter.notifyDataSetChanged();
		}
	}
	

}


